﻿using System.Data;
using KZ.Utilities;
using Process.Entities;
using System.Data.SqlClient;
using KZ.Exception;
using System.Collections.Generic;

namespace Process.DAL
{
    public class OrderDetailDAL
    {
        private static OrderDetailENT CreateOrderDetailFromReader(IDataReader reader)
        {
            OrderDetailENT item = new OrderDetailENT();

            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.OrderDetailID.ToString()))) item.OrderDetailID = Utility.StringParse(reader[OrderDetailColumns.OrderDetailID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.OrderID.ToString()))) item.OrderID = Utility.StringParse(reader[OrderDetailColumns.OrderID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.TransID.ToString()))) item.TransID = Utility.StringParse(reader[OrderDetailColumns.TransID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.ProductID.ToString()))) item.ProductID = Utility.StringParse(reader[OrderDetailColumns.ProductID.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.Quantity.ToString()))) item.Quantity = Utility.IntParse(reader[OrderDetailColumns.Quantity.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.UnitPrice.ToString()))) item.UnitPrice = Utility.DecimalParse(reader[OrderDetailColumns.UnitPrice.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.TransportPrice.ToString()))) item.TransportPrice = Utility.DecimalParse(reader[OrderDetailColumns.TransportPrice.ToString()]);
            if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.LineNet.ToString()))) item.LineNet = Utility.DecimalParse(reader[OrderDetailColumns.LineNet.ToString()]);

            DataTable dt = reader.GetSchemaTable();
            if (dt.IsColumnExist(OrderDetailColumns.ProductName.ToString()))
            {
                if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.ProductName.ToString()))) item.ProductName = Utility.StringParse(reader[OrderDetailColumns.ProductName.ToString()]);
            }
            if (dt.IsColumnExist(OrderDetailColumns.PartnerID.ToString()))
            {
                if (!reader.IsDBNull(reader.GetOrdinal(OrderDetailColumns.PartnerID.ToString()))) item.PartnerID = Utility.StringParse(reader[OrderDetailColumns.PartnerID.ToString()]);
            }

            return item;
        }

        public static int GetInventoryStockingQuantity(string transID)
        {
            try
            {
                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, transID);
                DataAccessHelper.AddParameter("StockingQuantity", SqlDbType.Int, 0, ParameterDirection.Output);

                DataAccessHelper.ExecuteNonQuery("procInventoryGetStockingQuantity");

                int stockingQuantity = Utility.IntParse(DataAccessHelper.GetParameterValue("StockingQuantity"));
                if (stockingQuantity == -1) { throw new DataAccessException("Giao dịch không tồn tại, không thể thực hiện việc kiểm tra tồn kho."); }
                return stockingQuantity;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy dữ liệu tồn kho.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public static List<OrderDetailENT> GetOrderDetailByOrderStatus(OrderStatus status, string providerID)
        {
            try
            {
                List<OrderDetailENT> orderDetailList = new List<OrderDetailENT>();

                DataAccessHelper.AddParameter("Status", SqlDbType.VarChar, status.ToString());
                DataAccessHelper.AddParameter("ProviderID", SqlDbType.VarChar, providerID);

                using (IDataReader reader = DataAccessHelper.ExecuteQuery("procOrderDetailGetByOrderStatus"))
                {
                    while (reader.Read())
                    {
                        orderDetailList.Add(CreateOrderDetailFromReader(reader));
                    }
                }

                return orderDetailList;
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình lấy dữ liệu tồn kho.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }

        public bool InsertOrderDetail(OrderDetailENT orderDetail)
        {
            try
            {
                DataAccessHelper.AddParameter("OrderDetailID", SqlDbType.VarChar, orderDetail.OrderDetailID);
                DataAccessHelper.AddParameter("OrderID", SqlDbType.VarChar, orderDetail.OrderID);
                DataAccessHelper.AddParameter("TransID", SqlDbType.VarChar, orderDetail.TransID);
                DataAccessHelper.AddParameter("ProductID", SqlDbType.VarChar, orderDetail.ProductID);
                DataAccessHelper.AddParameter("Quantity", SqlDbType.Int, orderDetail.Quantity);
                DataAccessHelper.AddParameter("UnitPrice", SqlDbType.Decimal, orderDetail.UnitPrice);
                DataAccessHelper.AddParameter("TransportPrice", SqlDbType.Decimal, orderDetail.TransportPrice);
                DataAccessHelper.AddParameter("LineNet", SqlDbType.Decimal, orderDetail.LineNet);

                if (DataAccessHelper.ExecuteNonQueryWithTransaction("procOrderDetailInsert") > 0)
                {
                    return true;
                }
                throw new DataAccessException("Có lỗi trong quá trình thêm mới đơn hàng.");
            }
            catch (SqlException)
            {
                throw new DataAccessException("Có lỗi trong quá trình thêm mới đơn hàng.");
            }
            finally
            {
                DataAccessHelper.ClearCommand();
            }
        }
    }
}